package com.cms.sync.service;

import java.util.ArrayList;
import java.util.List;

import com.cms.sync.model.AllQueryData;
import com.cms.sync.model.KpayAction;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class KpayActionServiceImpl extends AbstractDateService implements KpayActionService {

	@Override
	public boolean saveKpayAction(KpayAction t) {
		String sql="";
		KpayAction  l = getKpayAction(t);
		if(l!=null)
		{
			sql = "update t_kpayaction set " +
					"fee='"+(t.getFee()+l.getFee())+"'" +
					",newuser='"+(t.getNewuser()+l.getNewuser())+"'" +
					",activeuser='"+(t.getActiveuser()+l.getActiveuser())+"'" +
					",payuser='"+(t.getPayuser()+l.getPayuser())+"'" +
					",paynumber='"+(t.getPaynumber()+l.getPaynumber())+"'" +
					"where day='"+t.getDay()+"' " +
						  "and hour='"+t.getHour()+"' " +
						  "and minute='"+t.getMinute()+"' " +
						  "and cpid='"+t.getCpid()+"' " +
						  "and channelid='"+t.getChannelid()+"' " +
						  "and appversionnumber='"+t.getAppversionnumber()+"' " +
						  "and goodsid='"+t.getGoodsid()+"' " +
						  "and provider='"+t.getProvider()+"'";
		}else
		{
			t.setId(getTableId("t_importsyncdata"));
			sql = "insert into t_kpayaction (id,day,hour,minute,cpid,appversionnumber,channelid," +
					"goodsid,goodsname,provider,fee,newuser,activeuser,payuser,paynumber,createtime) values ("+t.getId()+"," +
					"'"+t.getDay()+"','"+t.getHour()+"','"+t.getMinute()+"','"+t.getCpid()+"'" +
					",'"+t.getAppversionnumber()+"','"+t.getChannelid()+"','"+t.getGoodsid()+"','"+
					t.getGoodsname()+"','"+t.getProvider()+"','"+
					t.getFee()+"','"+t.getNewuser()+"','"+t.getActiveuser()+"','"+t.getPayuser()+"','"+t.getPaynumber()+"','"+t.getCreatetime()+"')";
		}
		logger.info("[SQL] [method:saveKpayAction] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public KpayAction getKpayAction(KpayAction t) {
		String sql = "select * from t_kpayaction where" +
				" day='"+t.getDay()+"'" +
				" and hour='"+t.getHour()+"'" +
				" and minute='"+t.getMinute()+"'" +
				" and cpid='"+t.getCpid()+"'" +
				" and channelid='"+t.getChannelid()+"'" +
				" and appversionnumber='"+t.getAppversionnumber()+"'" +
				" and goodsid='"+t.getGoodsid()+"'" +
				" and provider='"+t.getProvider()+"'";
		ModelGenerator<KpayAction> dbcpmg = new ModelGenerator<KpayAction>(KpayAction.class);
		logger.info("[SQL] [method:getKpayAction] ["+sql+"]");
		return dbcpmg.generate(base, sql);
	}

	@Override
	public List<AllQueryData> getAllData(String startday, String endday,
			AllQueryData model, String dimension) {
		List<String> fl = new ArrayList<String>();
		String ch="";
		if(checkQueryDimension(dimension, "day")){
			ch+="t.day day,";
			fl.add("t.day");
		}
		if(checkQueryDimension(dimension, "channel")){
			ch+="t.channelid channelid,";
			fl.add("t.channelid");
		}
		if(checkQueryDimension(dimension, "app")){
			ch+="t.appversionnumber appversionnumber,";
			fl.add("t.appversionnumber");
		}
		if(checkQueryDimension(dimension, "operator")){
			ch+="t.operator operator,";
			fl.add("t.operator");
		}
		
		String sql = "select "+ch+" sum(t.newusers) newuser,sum(t.activeuser) activeuser,sum(t.payingcustomers) payuser,sum(t.paidnumber) paynum,sum(t.fee) fee," +
				"r.newuserkpay newuserkpay,r.activeuserkpay activeuserkpay,r.payuserkpay payuserkpay,r.paynumkpay paynumkpay,r.feekpay/100 feekpay " +
				"from t_importdata t left join (select a.day day,a.operatorname operatorname,a.channelid channelid,a.appversionnumber appversionnumber, sum(a.newuser) newuserkpay,sum(a.activeuser) activeuserkpay,count(distinct b.imsi) payuserkpay,count(b.imsi) paynumkpay,sum(b.orderamount) feekpay " +
					"from t_kpaysynclog b,t_kpayusernum a where a.day=b.day and a.operatorname=b.operatorname and a.channelid=b.channelid and a.appversionnumber=b.appversionnumber and b.resultcode='100000' and a.day>="+startday+" and a.day<="+endday+" group by a.day,a.operatorname,a.channelid,a.appversionnumber) r " +
						"on (t.day=r.day and t.operator=r.operatorname and t.channelid=r.channelid and t.appversionnumber=r.appversionnumber) ";
		
		String w=" where t.day>="+startday+" and t.day<="+endday;
		if(model.getChannelid()>0){
			w+=" and t.channelid="+model.getChannelid();
		}
		if(model.getAppversionnumber()!=null&&!"".equals(model.getAppversionnumber())){
			w+=" and t.appversionnumber='"+model.getAppversionnumber()+"'";
		}
		if(model.getOperator()!=null&&!"".equals(model.getOperator())){
			w+=" and t.operator='"+model.getOperator()+"'";
		}
		
		sql+=w+" group by "+fl.toString().replace("[", "").replace("]", "").replace(" ", "");
		
		logger.info("[sql] [method:getAllData] ["+sql+"]");
		ModelGenerator<AllQueryData> m = new ModelGenerator<AllQueryData>(AllQueryData.class);
		return m.generateList(base,sql.toString());
	}

}
